/** @format */

import axios from '../../src/index';

axios({
  url: '/extend/post',
  method: 'post',
  data: {
    msg: 'hi',
  },
});

// 重载实现
axios('/extend/post', {
  method: 'post',
  data: {
    msg: '大爷常来玩儿啊',
  },
});

axios.request({
  url: '/extend/post',
  method: 'post',
  data: {
    msg: 'hello',
  },
});

axios.get('/extend/get');
axios.options('/extend/options');
axios.delete('/extend/delete');
axios.patch('/extend/patch', {msg: 'patch'});
axios.put('/extend/put', {msg: 'put'});

// responseData泛型
interface ResponseData<T = any> {
  code: number;
  result: T;
  message: string;
}

interface User {
  name: string;
  age: number;
}

function getUser<T>() {
  return axios<ResponseData<T>>('/extend/user')
    .then(res => res.data)
    .catch(err => console.error(err));
}

async function test() {
  const user = await getUser<User>();
  if (user) {
    console.log(user.result.name + ':' + user.result.age);
  }
}

test();
